using System;
using System.Collections;
using System.Collections.Generic;
using QFramework;
using QFramework.Example;
using UnityEngine;
using UnityEngine.UI;

public class UIItemTip : MonoBehaviour
{
    public GameObject TipPanel;
    public Image Icon;
    public Text Name;
    public Text Description;

    /// <summary>
    /// 设置单例
    /// </summary>
    private static UIItemTip mDefault;

    public static void Show(UISlot slot)
    {
        //Debug.Log("进入Show方法");
        if (slot.Data.Item!=null&&slot.Data.Count>0)
        {
            //Debug.Log("Item不为空，准备展示");
            mDefault.Icon.sprite = slot.Data.Item.GetIcon;
            mDefault.Name.text = slot.Data.Item.GetName;
            mDefault.Description.text = slot.Data.Item.GetDescription;
            mDefault.TipPanel.Show();

            // 把tipsPanel界面的pivot点设置为最下面的中心位置
            // 展示的时候要让tips界面跟随鼠标位置
            // 获得当前鼠标位置
            var mousePos = Input.mousePosition;
            if (RectTransformUtility.ScreenPointToLocalPointInRectangle(
                    mDefault.TipPanel.transform.parent as RectTransform, mousePos, null, out var localPos))
            {
                const int Offset = 40;
                // 根据当前鼠标位置及屏幕中心点，判断tip显示的区域，比如在左边，则tip要显示在鼠标右边，在下边，tip则要显示在鼠标上边
                var mousePosToCenter=mousePos-new Vector3(Screen.width*0.5f,Screen.height*0.5f,0);
                if (mousePosToCenter.x.Abs()>mousePosToCenter.y.Abs())
                {
                    if (mousePosToCenter.x<0)
                    {
                        mDefault.TipPanel.GetComponent<RectTransform>().pivot=new Vector2(0,0.5f);
                        mDefault.TipPanel.LocalPosition(localPos+Vector2.right*Offset);
                    }
                    else
                    {
                        mDefault.TipPanel.GetComponent<RectTransform>().pivot=new Vector2(1,0.5f);
                        mDefault.TipPanel.LocalPosition(localPos+Vector2.left*Offset);
                    }
                }
                else
                {
                    if (mousePosToCenter.y<0)
                    {
                        mDefault.TipPanel.GetComponent<RectTransform>().pivot=new Vector2(0.5f,0);
                        mDefault.TipPanel.LocalPosition(localPos+Vector2.up*Offset);
                    }
                    else
                    {
                        mDefault.TipPanel.GetComponent<RectTransform>().pivot=new Vector2(0.5f,1);
                        mDefault.TipPanel.LocalPosition(localPos+Vector2.down*Offset);
                    }
                }
                
                //mDefault.TipPanel.LocalPosition(localPos+Vector2.up*80);
            }
        }
    }

    public static void Hide()
    {
        mDefault.TipPanel.Hide();
    }
    
    private void Awake()
    {
        mDefault = this;
    }

    private void Start()
    {
        mDefault.TipPanel.Hide();
    }

    private void OnDestroy()
    {
        mDefault = null;
    }
}
